package com.gc.server.controller.api;

import com.gc.server.entity.ScoreEntity;
import com.gc.server.entity.ScoreItemEntity;
import com.gc.server.entity.TodoEntity;
import com.gc.server.entity.UserEntity;
import com.gc.server.service.ScoreItemService;
import com.gc.server.service.ScoreService;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/score")
public class ApiScoreController {

    @Autowired
    private ScoreService scoreService;

    @PostMapping("/")
    public R scoreVisit(@LoginUserId Long userId, Long todoId, String[] names, BigDecimal[] points, String[] remarks) {
        scoreService.score(todoId, names, points, userId, remarks);
        return R.ok();
    }

    @Autowired
    private ScoreItemService itemService;

    @GetMapping("/{todoId}")
    public R list(@LoginUserId Long userId, @PathVariable Long todoId) {
        List<ScoreEntity> scoreEntities = scoreService.findByTodoId(todoId);
        List<ScoreItemEntity> items = itemService.find(null);
        Map scoreItemMap = new HashMap();
        items.forEach(scoreItemEntity -> {
            scoreItemMap.put(scoreItemEntity.getVal(),scoreItemEntity);
        });
        Map scoreMap = new HashMap();
        scoreEntities.forEach(scoreEntity -> {
            scoreMap.put(scoreEntity.getName(),scoreEntity);
        });
        return R.ok(scoreEntities).putExtra("scoreMap",scoreMap).putExtra("scoreItemMap",scoreItemMap);
    }


}
